home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 December / PSL Monthly Shareware CD-ROM (Public Software Library)(December 1994).bin / prgmming / dos / c1 / newgetch.c < prev    next >
Text File  |  1991-10-01  |  2KB  |  119 lines

  1. /*
  2. GETKEY( void )
  3.  
  4. This function can be used as a direct replacement for the getch()
  5. function in the Microsoft C and QuickC runtime libraries.
  6.  
  7. The original Microsoft functions did not properly report ALT-Q
  8. or ALT-R combinations, but instead activated their respective
  9. DOS functions of 'printer redirection' and 'pause'.
  10.  
  11. GETKEY() functions exactly like the getch() function in that
  12. it must be called twice to retrieve extended keycodes.
  13.  
  14. Any suggestions are welcomed.  Please send to CIS 72520,3710
  15.  
  16.  
  17. Copyright (c) 1991 RWR Consulting
  18. ALL RIGHTS RESERVED
  19.  
  20. Placed in the public domain October, 1991
  21.  
  22. May be freely distributed.
  23.  
  24.  
  25. */
  26.  
  27.  
  28. /*  INCLUDE FILES REQUIRED:     bios.h  ctype.h   */
  29.  
  30.  
  31.  
  32. int getkey( void )
  33. {
  34.  
  35.     /* Static vars are used to eliminate redundant memory peeks
  36.        and constant setting of _KEYBRD values */
  37.  
  38.  
  39.     /* Additionally, to function as getch() does, the scan code
  40.        must be preserved and flagged for subsequent calls */
  41.  
  42.  
  43.     unsigned key, scan, ascii;
  44.  
  45.     static int kread = _KEYBRD_READ;
  46.     static int kready = _KEYBRD_READY;
  47.     static int kshiftstatus = _KEYBRD_SHIFTSTATUS;
  48.  
  49.     static int initflag = 1;
  50.     static int extended = 0;
  51.     static unsigned int nextkey = 0;
  52.  
  53.  
  54.  
  55.     /* If bit 4  (& 0x10) of byte at 0x0040:0x0096 is set (See Norton),
  56.        a new keyboard is present.  */
  57.  
  58.  
  59.     if(initflag)    /* only do it once */
  60.     {
  61.         if( (*(unsigned char _far *)0x00400096 ) & 0x10 )
  62.         {
  63.  
  64.             /* Setup for enhanced (new) keyboards */
  65.  
  66.             kread = _NKEYBRD_READ;
  67.             kready = _NKEYBRD_READY;
  68.             kshiftstatus = _NKEYBRD_SHIFTSTATUS;
  69.         }
  70.  
  71.         initflag = 0;   /* and never again */
  72.     }
  73.  
  74.  
  75.  
  76.     /* If an extended code (see below), return the extended portion
  77.        and reset the flag */
  78.  
  79.     if(extended)
  80.     {
  81.         extended = 0;
  82.         return((int)nextkey);
  83.     }
  84.  
  85.  
  86.     key = _bios_keybrd( kread );
  87.  
  88.     /* Split return value into the scan and ascii codes. */
  89.  
  90.     scan = key >> 8;
  91.     ascii = key & 0x00ff;
  92.  
  93.  
  94.     /* Determine if the key was an extended one */
  95.  
  96.     if( (ascii == 0) || (ascii == 0xE0 ) )
  97.     {
  98.         /* If so, set the flag and return the 0x00 or 0xE0 to
  99.            the calling function and save the second half for later */
  100.  
  101.         extended = 1;
  102.         nextkey = scan;
  103.     }
  104.     else
  105.     {
  106.         /* Otherwise, just reset the flag */
  107.  
  108.         extended = 0;
  109.     }
  110.  
  111.  
  112.     /* Return the first code; may be 0x00 or 0xE0 if extended */
  113.  
  114.     return((int)ascii);
  115.  
  116. }
  117.  
  118.  
  119.